// pages/about/index.js

const { storeList } = require('../../utils/api')
const app = getApp()
Page({
    data: {
        active: 0,
        typeList: [],
        list: [],
        num: 1,
        hasMore: true,
        loadFlag: true,
        typetwo: 1,
        show: false,
        title: '',
        columns: []
    },

    onLoad: function(options) {
        const data = JSON.parse(options.data)
        let columns = app.globalData.merchantsTypeList.map((item, index) => {
            if (item.id === data.id) {
                this.setData({
                    active: index
                })
            }
            return item.title
        })
        this.setData({
            columns,
            typeList: app.globalData.merchantsTypeList
        })
        this.setTitle()
        this.getData()
        wx.hideShareMenu()
    },
    setTitle() {
        this.setData({
            title: app.globalData.merchantsTypeList[this.data.active].title
        })
        wx.setNavigationBarTitle({
            title: this.data.title
        })
    },
    getData() {
        wx.showNavigationBarLoading()
        wx.showLoading({
            title: '加载中...'
        })

        storeList({
            pageSize: 20,
            pageNumber: this.data.num,
            type: this.data.typeList[this.data.active].id,
            typetwo: this.data.typetwo
        }).then(res => {
            wx.hideNavigationBarLoading()
            wx.hideLoading()
            wx.stopPullDownRefresh()

            if (this.data.num == 1) {
                this.setData({
                    list: []
                })
            }
            this.setData({
                list: this.data.list.concat(res.list),
                loadFlag: true
            })
            if (res.list.length < 20) {
                this.setData({
                    hasMore: false
                })
            }
        })
    },
    showFn() {
        this.setData({
            show: !this.data.show
        })
    },
    onChange(e) {
        this.setData({
            typetwo: e.currentTarget.dataset.id,
            num: 1,
            hasMore: true,
            loadFlag: true
        })
        this.getData()
    },
    onConfirm(e) {
        this.setData({
            title: e.detail.value,
            active: e.detail.index,
            show: false,
            num: 1,
            hasMore: true,
            loadFlag: true
        })
        this.getData()
    },
    onReady: function() {},
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        if (this.data.hasMore && this.data.loadFlag) {
            this.setData({
                pageNumber: this.data.pageNumber + 1,
                loadFlag: false
            })
            this.getData()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {}
})
